<?php
/**
 * Created by PhpStorm.
 * User: 睿
 * Date: 2017-11-03
 * Time: 17:31
 */

namespace app\store\controller;


use controller\BasicAdmin;
use service\DataService;
use think\Db;

class Material_old extends BasicAdmin
{
    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'StoreMaterial';

    /**
     * @Author  lingyun
     * @Desc    一键登录门店
     * 2018-05-04
     */
    public function _initialize()
    {
        //@lingyun   2018-01-04  一键登入门店
        if(!empty(input('storeid'))){           //门店id
            session('user.store_id',input('storeid'));
        }
        //@lingyun   2018-01-04  一键登入门店
    }
    /**
     * 订货单列表
     * @return array
     */
    public function index()
    {
        $this->title = '待订货原料列表（总部）';
        $get = $this->request->get();

        $store_id = session('user')['store_id'];

        $join = [
            ['ys_goods_material g', 'a.material_id=g.id', 'LEFT'],
            ['ys_goods_material_category c', 'g.cat_id=c.id', 'LEFT'],
        ];

        $db = Db::name($this->table)->alias('a')->where('a.store_id', $store_id)->where('a.is_deleted', 0)->where('a.status', 0)->where('type',0)->field('g.name,c.name as cname,a.id,a.addtime,g.unit,a.num')->join($join);
        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }

        $qiniu = config('qiniu');
        $this->assign('qiniu', $qiniu['yangshi']);          //七牛样式后缀

        return parent::_list($db);

    }

    /**
     * 添加原材料
     * @return array|string
     */
    public function add()
    {
        $store_id = session('user')['store_id'];
        if (request()->ispost()) {
            $insert = db($this->table)->insert(['material_id' => input('material_id'), 'num' => input('num'), 'store_id' => $store_id, 'addtime' => time()]);
            if ($insert !== false)
                $this->success('原料添加成功！', '');
        } else {
            $db = db('goods_material')->where('is_deleted', 0)->where('is_zigou',0)->select();
            $this->assign('list', $db);

            return $this->_form($this->table, 'form');
        }
    }

    /**
     * 根据配方添加原材料
     * @return array|string
     */
    public function plus()
    {
        $store_id = session('user')['store_id'];
        if (request()->ispost()) {
            $yongliao = db('goods_yongliao')->where('goods_id', input('goods_id'))->select();

            foreach ($yongliao as $value)
                db($this->table)->insert(['material_id' => $value['material_id'], 'num' => $value['num']*input('num'), 'store_id' => $store_id, 'addtime' => time()]);

            $this->success('原料添加成功！', '');
        } else {
            $db = db('goods')->where('is_deleted', 0)->select();
            $this->assign('list', $db);

            return $this->_form($this->table, 'plus');
        }
    }

    /**
     * 编辑原材料
     * @return array|string
     */
    public function edit()
    {
        $db = db('goods_material')->where('is_deleted', 0)->select();

        $this->assign('list', $db);

        return $this->_form($this->table, 'form');
    }


    /**
     * 删除原材料
     */
    public function del()
    {
        if (DataService::update($this->table)) {
            $this->success("原材料删除成功！", '');
        }
        $this->error("原材料删除失败，请稍候再试！");
    }

    /**
     * 生成订货单
     * @throws \think\Exception
     */
    public function order()
    {
        if (DataService::update($this->table)) {
            $sn = build_order_no();
            $store_id = session('user')['store_id'];
            $i = db('store_order')->insertGetId(['sn' => $sn, 'addtime' => time(), 'store_id' => $store_id]);

            $ids = input('id');
            $ids = explode(',', $ids);
            //遍历原材料
            foreach ($ids as $id){
                db($this->table)->where('id', $id)->update(['order_id' => $i]);
                $store_material = db($this->table)->where('id',$id)->find();
                if (db('store_material_order')->where(['order_id'=>$i,'material_id'=>$store_material['material_id']])->find())
                    db('store_material_order')->where(['order_id'=>$i,'material_id'=>$store_material['material_id']])->setInc('num',$store_material['num']);
                else
                    db('store_material_order')->insert(['order_id'=>$i,'material_id'=>$store_material['material_id'],'num'=>$store_material['num'],'store_id'=>$store_id,'addtime'=>time()]);
            }

            $this->success("订货单生成成功！", '');
        }
        $this->error("订货单生成失败，请稍候再试！");
    }


}